<template>
	<view>
		<view class="info">
			<image src="/static/images/mine/head2.png" class="head-img" v-if="info.sex==2"></image>
			<image src="/static/images/mine/head.png" class="head-img" v-else></image>
			<!-- <image src="/static/images/mine/head.png"></image> -->
			<button v-if="info==''" style="padding: 0;" class="tab" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">点击登录</button>
			<!-- <navigator url="/pages/login/login" hover-class="none" v-if="info==''">请登录</navigator> -->
			<view v-else>{{info.realname==null||info.realname==''?'微信用户':info.realname}}</view>
		</view>
		<view class="main">
			<view class="card">
				<view class="item flex align between">
					<view class="name flex align">
						姓名
					</view>
					<view>{{info.realname}}</view>
				</view>
				<view class="item flex align between">
					<view class="name flex align">
						性别
					</view>
					<view>{{info.sex_text}}</view>
				</view>
				<view class="item flex align between">
					<view class="name flex align">
						流出党支部
					</view>
					<view>{{info.out_organization_text}}</view>
				</view>
				<view class="item flex align between">
					<view class="name flex align">
						流入党支部
					</view>
					<view>{{info.in_organization_text}}</view>
				</view>
				<view class="item flex align between">
					<view class="name flex align">
						操作手册
					</view>
					<navigator url="/pages/profile/book" hover-class="none"
						style="color:#C23015;text-decoration: underline;">点击查看</navigator>
				</view>
				<view class="item flex align between">
					<view class="name flex align">
						订阅消息
					</view>
					<view @click="toBook()"
						style="background:#00aaff;border: solid 1rpx;color: white;padding: 15rpx 30rpx;font-size: 30rpx;border-radius: 8rpx;">
						点击订阅</view>
				</view>
			</view>
		</view>

	</view>
</template>
<script>
	import {
		userDetail,
		wxlogin,
		userLogin
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				info: '',
				list: []
			}
		},
		onLoad() {

		},
		onShow() {
			let token = uni.getStorageSync('token')
			let that = this
			uni.request({
				url: 'https://xsdy.ncscloudmedia.com/index.php/api/user/checkToken',
				data: {},
				method: 'POST',
				header: {
					'content-type': 'application/json',
					'token': token
				},
				success: result => {
					if (result.data.code == 1) {
						that.getDetail()
					} else {

					}
				},
				fail: result => {
					uni.hideLoading();
				}
			})
		},

		onReady() {

		},
		onShareAppMessage(res) {

		},
		methods: {
			// 订阅消息
			toBook() {
				let that = this
				wx.getSetting({
					withSubscriptions: true,
					success(res) {
						let templateId = "eAvm1oL34Z7_IVf4D7jblQsaWvXYCfR8lIKe31nft-0"; //替换为你的模板ID
						let subscriptionStatus = res.subscriptionsSetting[templateId];
						if (subscriptionStatus === "accept") {
							//用户已订阅
							console.log("用户已订阅");
							uni.showToast({
								title:'您已订阅该消息',
								icon:'none',
							})
							// uni.showModal({
							// 	title: '提示',
							// 	content: '您已订阅该消息',
							// 	showCancel: false,
							// 	success: function(res) {
							// 		if (res.confirm) {
							// 			// console.log('用户点击确定');
							// 		} else if (res.cancel) {
							// 			console.log('用户点击取消');
							// 		}
							// 	}
							// });
						}else if(subscriptionStatus === "reject") {
							//用户拒绝订阅
							console.log("用户拒绝订阅");
							uni.showToast({
								title: "你已拒绝订阅",
								icon: "error",
							});
							that.toBookCom()
						}else if(subscriptionStatus === "ban") {
							//订阅消息被封禁
							uni.showToast({
								title: "订阅消息被封禁",
								icon: "error",
							});
							console.log("订阅消息被封禁");
							that.toBookCom()
						} else {
							//用户未订阅或未授权
							console.log("用户未订阅或未授权");
							that.toBookCom()
						}

					}

				});
			},
			toBookCom() {
				let id = "eAvm1oL34Z7_IVf4D7jblQsaWvXYCfR8lIKe31nft-0"; // 替换你自己的模板ID 模板详情那里就有模板ID
				// 调用订阅消息API
				let that = this
				uni.requestSubscribeMessage({
					tmplIds: [id], // 这里填写你的模板ID数组
					success(res) {
						if (res[id] === 'accept') {
							// 用户同意订阅该模板
							uni.showToast({
								title: "订阅成功",
								icon: "success",
							});
						}
						if (res[id] === 'reject') {
							// 用户拒绝订阅该模板
							uni.showModal({
								title: "订阅失败",
								content: "您已设置不再询问，请前往设置开启订阅提醒。",
								confirmText: "去设置",
								success(modalRes) {
									if (modalRes.confirm) {
										that.openSetting();
									} else {
										uni.showToast({
											title: "您可能错过重要通知",
											icon: "none",
										});
									}
								},
							});
						}
					},
					fail(err) {
						uni.showToast({
							title: "订阅消息失败",
							icon: "error",
						});
						console.error('订阅消息失败', err);
					}
				});

			},
			openSetting(){
				 uni.openSetting({
				    success(res) {
				      console.log("设置界面返回：", res);
				      if (res.subscriptionsSetting) {
				        console.log("订阅消息授权状态：", res.subscriptionsSetting);
				      }
				    },
				    fail(err) {
				      console.error("打开设置失败：", err);
				    },
				  });
			},
			async getPhoneNumber(e) {
				if (e.detail.code != null) {
					this.getPhone(e.detail.code)
				}
			},
			getPhone(e) {
				let code2 = e
				console.log(code2)
				let that = this
				uni.login({
					provider: 'weixin',
					success(res) {
						console.log(res)
						let code1 = res.code
						uni.getUserInfo({
							provider: 'weixin',
							success: (res) => {
								console.log(res)
								var encryptedData = res.encryptedData;
								var iv = res.iv
								var info = res.rawData
								var obj = {
									code2: code2,
									code1: code1,
									iv: iv,
									encryptedData: encryptedData
								}
								that.toLogin(obj)
							},
							fail: res => {
								console.log(res)

							}
						})
					}
				})
			},
			async toLogin(obj) {
				const result = await userLogin(obj)
				if (result.code == 1) {
					uni.showToast({
						title: result.msg,
						icon: 'success',
						mask: true
					});
					uni.setStorageSync('token', 'Dearer ' + result.data.token);
					uni.setStorageSync('type', 1);
					this.getDetail()
					// uni.navigateTo({
					// 	url: '/pages/apply/apply'
					// })
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					});
				}
			},
			toUrl(u) {
				if (this.info == '') {
					uni.showToast({
						title: '请登录',
						icon: 'error'
					})

				} else {
					uni.navigateTo({
						url: u
					})
				}

			},
			async getDetail() {
				const result = await userDetail()
				if (result.code == 1) {
					this.info = result.data
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'error'
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.info {
		text-align: center;
		padding: 30rpx 0 20rpx;

		image {
			width: 128rpx;
			height: 128rpx;
		}

		.tab {
			background: linear-gradient(to bottom, #FAD705, #FF6C02);
			border-radius: 40rpx;
			width: 144rpx;
			line-height: 48rpx;
			color: white;
			text-align: center;
			font-size: 24rpx;
			margin: 20rpx auto;
			// box-shadow: 1rpx 2rpx 6rpx 0rpx #2d2d2d;
		}
	}

	.main {
		margin: 20rpx 44rpx;
	}

	.title {
		color: #C23015;
		border-left: solid;
		padding-left: 13rpx;
	}

	.card {
		background: white;
		border-radius: 8rpx;
		margin-top: 24rpx;
		font-size: 28rpx;
		color: #747474;

		.item {
			padding: 22rpx 24rpx;
			border-bottom: solid 1rpx #F6F6F6;

			image {
				width: 24rpx;
				height: 24rpx;
				margin-right: 20rpx;
			}
		}
	}
</style>